package view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import model.bean.Employee;

public class MainView extends JFrame{

	private Employee curEmp = null;
	private int EmployeeId;
	
	private JButton btnDepartmentList;
	private JButton btnDepartmentManage;
	private JButton btnEmpMyProfile;
	private JButton btnEmpTransferManage;
	private JButton btnEmployeeManage;
	private JButton btnHomeEmp;
	private JButton btnLocationList;
	private JButton btnLocationManage;
	private JButton btnLogout1;
	private JButton btnProjectList;
	private JButton btnProjectManage;
	private JButton btnRequestTransfer;
	
	private JMenuBar 	menuBarMain;
	private JMenu	 	menuFile;
	private JMenuItem	menuItemExit;
	private JMenuItem	menuItemLogout;
	
	private JPanel 	pnlHome;
	private JPanel	pnlLeft;
	private JPanel	pnlLeftEmp;
	
	private JScrollPane scrlpLeftUser;
	private JScrollPane	srclpLeftAdmin;
	private JSplitPane splPane;
	
	
	//create new from MainView
	public MainView(Employee emp){
		this.curEmp = emp;
		
		initComponents();
		setFrameStyle();
		
		if(this.curEmp.isAmin()){
			setAdminLayout();
			
		}else{
			setUserLayout();
		}
	}
	
	private void initComponents(){
		scrlpLeftUser = new JScrollPane();
	    pnlLeftEmp = new JPanel();
	    btnEmpMyProfile = new JButton();
        btnLogout1 = new JButton();
        btnEmpTransferManage = new JButton();
        btnRequestTransfer = new JButton();
        btnHomeEmp = new JButton();
        btnDepartmentList = new JButton();
        btnProjectList = new JButton();
        btnLocationList = new JButton();
        splPane = new JSplitPane();
        srclpLeftAdmin = new JScrollPane();
        pnlLeft = new JPanel();
        btnProjectManage = new JButton();
        btnDepartmentManage = new JButton();
        btnLocationManage = new JButton();
        btnEmployeeManage = new JButton();
        pnlHome = new JPanel();
        menuBarMain = new JMenuBar();
        menuFile = new JMenu();
        menuItemLogout = new JMenuItem();
        menuItemExit = new JMenuItem();
        
        scrlpLeftUser.setMaximumSize(null);
        scrlpLeftUser.setMinimumSize(null);
        scrlpLeftUser.setPreferredSize(new Dimension(225, 235));

        pnlLeftEmp.setMaximumSize(new Dimension(185, 539));
        pnlLeftEmp.setMinimumSize(new Dimension(185, 539));
        pnlLeftEmp.setPreferredSize(new Dimension(185, 539));

        btnEmpMyProfile.setIcon(new ImageIcon(getClass().getResource("/images/project_open.png"))); 
        btnEmpMyProfile.setText("My Profile");
        btnEmpMyProfile.setHorizontalAlignment(SwingConstants.LEFT);
        btnEmpMyProfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnEmpMyProfileActionPerformed(e);	
			}
		});
        
        btnLogout1.setIcon(new ImageIcon(getClass().getResource("/images/logout.png"))); 
        btnLogout1.setText("Logout");
        btnLogout1.setHorizontalAlignment(SwingConstants.LEFT);
        btnLogout1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnLogout1ActionPerformed(evt);
            }
        });
        
        btnEmpTransferManage.setIcon(new ImageIcon(getClass().getResource("/images/Transfermanage.png"))); 
        btnEmpTransferManage.setText("My Transfers Manage");
        btnEmpTransferManage.setHorizontalAlignment(SwingConstants.LEFT);
        btnEmpTransferManage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnEmpTransferManageActionPerformed(evt);
            }
        });
        
        btnRequestTransfer.setIcon(new ImageIcon(getClass().getResource("/images/transferNew.png"))); 
        btnRequestTransfer.setText("Request New Transfer");
        btnRequestTransfer.setHorizontalAlignment(SwingConstants.LEFT);
        btnRequestTransfer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnRequestTransferActionPerformed(evt);
            }
        });
        
        btnHomeEmp.setIcon(new ImageIcon(getClass().getResource("/images/home.png"))); // NOI18N
        btnHomeEmp.setText("Home");
        btnHomeEmp.setHorizontalAlignment(SwingConstants.LEFT);
        btnHomeEmp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnHomeEmpActionPerformed(evt);
            }
        });
        
        btnDepartmentList.setIcon(new ImageIcon(getClass().getResource("/images/Department.png"))); // NOI18N
        btnDepartmentList.setText("Departments List");
        btnDepartmentList.setHorizontalAlignment(SwingConstants.LEFT);
        btnDepartmentList.setMaximumSize(new Dimension(101, 41));
        btnDepartmentList.setMinimumSize(new Dimension(101, 41));
        btnDepartmentList.setPreferredSize(new Dimension(79, 25));
        btnDepartmentList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnDepartmentListActionPerformed(evt);
            }
        });
        
        btnProjectList.setIcon(new ImageIcon(getClass().getResource("/images/project_open.png"))); // NOI18N
        btnProjectList.setText("Projects List");
        btnProjectList.setHorizontalAlignment(SwingConstants.LEFT);
        btnProjectList.setMaximumSize(new Dimension(101, 41));
        btnProjectList.setMinimumSize(new Dimension(101, 41));
        btnProjectList.setPreferredSize(new Dimension(79, 25));
        btnProjectList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnProjectListActionPerformed(evt);
            }
        });
        
        btnLocationList.setIcon(new ImageIcon(getClass().getResource("/images/Location.png"))); // NOI18N
        btnLocationList.setText("Locations List");
        btnLocationList.setHorizontalAlignment(SwingConstants.LEFT);
        btnLocationList.setMaximumSize(new Dimension(101, 41));
        btnLocationList.setMinimumSize(new Dimension(101, 41));
        btnLocationList.setPreferredSize(new Dimension(79, 25));
        btnLocationList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnLocationListActionPerformed(evt);
            }
        });
        
        GroupLayout pnlLeftEmpLayout = new GroupLayout(pnlLeftEmp);
        pnlLeftEmp.setLayout(pnlLeftEmpLayout);
        pnlLeftEmpLayout.setHorizontalGroup(
            pnlLeftEmpLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(pnlLeftEmpLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlLeftEmpLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(btnHomeEmp, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRequestTransfer, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEmpTransferManage, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlLeftEmpLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addComponent(btnProjectList, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnDepartmentList, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnLocationList, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(33, Short.MAX_VALUE))
            .addGroup(pnlLeftEmpLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlLeftEmpLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(btnEmpMyProfile, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLogout1, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        
        pnlLeftEmpLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {btnDepartmentList, btnEmpMyProfile, btnEmpTransferManage, btnHomeEmp, btnLocationList, btnLogout1, btnProjectList, btnRequestTransfer});

        pnlLeftEmpLayout.setVerticalGroup(
            pnlLeftEmpLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(pnlLeftEmpLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnHomeEmp)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnRequestTransfer)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnEmpTransferManage)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnProjectList, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnDepartmentList, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnLocationList, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnEmpMyProfile)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnLogout1)
                .addContainerGap(258, Short.MAX_VALUE))
        );
        
        pnlLeftEmpLayout.linkSize(SwingConstants.VERTICAL, new Component[] {btnDepartmentList, btnEmpMyProfile, btnEmpTransferManage, btnHomeEmp, btnLocationList, btnLogout1, btnProjectList, btnRequestTransfer});

        scrlpLeftUser.setViewportView(pnlLeftEmp);

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Employee Transfer Management");
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
		
        splPane.setDividerLocation(220);
        splPane.setDividerSize(0);
        splPane.setEnabled(false);
        splPane.setMaximumSize(null);
        splPane.setPreferredSize(new Dimension(1024, 543));

        srclpLeftAdmin.setMaximumSize(null);
        srclpLeftAdmin.setMinimumSize(null);
        srclpLeftAdmin.setPreferredSize(new Dimension(219, 541));

        pnlLeft.setMaximumSize(new Dimension(185, 539));
        pnlLeft.setMinimumSize(new Dimension(185, 539));
        pnlLeft.setPreferredSize(new Dimension(185, 539));

        btnProjectManage.setIcon(new ImageIcon(getClass().getResource("/images/project_open.png"))); // NOI18N
        btnProjectManage.setText("Project Manage");
        btnProjectManage.setHorizontalAlignment(SwingConstants.LEFT);
        btnProjectManage.setMaximumSize(new Dimension(101, 41));
        btnProjectManage.setMinimumSize(new Dimension(101, 41));
        btnProjectManage.setPreferredSize(new Dimension(79, 25));
        btnProjectManage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnProjectManageActionPerformed(evt);
            }
        });
        
        btnDepartmentManage.setIcon(new ImageIcon(getClass().getResource("/images/Department.png"))); // NOI18N
        btnDepartmentManage.setText("Department Manage");
        btnDepartmentManage.setHorizontalAlignment(SwingConstants.LEFT);
        btnDepartmentManage.setMaximumSize(new Dimension(101, 41));
        btnDepartmentManage.setMinimumSize(new Dimension(101, 41));
        btnDepartmentManage.setPreferredSize(new Dimension(79, 25));
        btnDepartmentManage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnDepartmentManageActionPerformed(evt);
            }
        });
        
        btnLocationManage.setIcon(new ImageIcon(getClass().getResource("/images/Location.png"))); // NOI18N
        btnLocationManage.setText("Location Manage");
        btnLocationManage.setHorizontalAlignment(SwingConstants.LEFT);
        btnLocationManage.setMaximumSize(new Dimension(101, 41));
        btnLocationManage.setMinimumSize(new Dimension(101, 41));
        btnLocationManage.setPreferredSize(new Dimension(79, 25));
        btnLocationManage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnLocationManageActionPerformed(evt);
            }
        });
        
        btnEmployeeManage.setIcon(new ImageIcon(getClass().getResource("/images/employee.png"))); // NOI18N
        btnEmployeeManage.setText("Employee Manage");
        btnEmployeeManage.setHorizontalAlignment(SwingConstants.LEFT);
        btnEmployeeManage.setMaximumSize(new Dimension(101, 41));
        btnEmployeeManage.setMinimumSize(new Dimension(101, 41));
        btnEmployeeManage.setPreferredSize(new Dimension(79, 25));
        btnEmployeeManage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                btnEmployeeManageActionPerformed(evt);
            }
        });
        
        GroupLayout pnlLeftLayout = new GroupLayout(pnlLeft);
        pnlLeft.setLayout(pnlLeftLayout);
        pnlLeftLayout.setHorizontalGroup(
            pnlLeftLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(pnlLeftLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlLeftLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(btnProjectManage, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDepartmentManage, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLocationManage, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEmployeeManage, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        pnlLeftLayout.setVerticalGroup(
            pnlLeftLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(pnlLeftLayout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(btnEmployeeManage, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addComponent(btnProjectManage, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDepartmentManage, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnLocationManage, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(833, Short.MAX_VALUE))
        );
        
        pnlLeftLayout.linkSize(SwingConstants.VERTICAL, new Component[] {btnDepartmentManage, btnEmployeeManage, btnLocationManage, btnProjectManage});

        srclpLeftAdmin.setViewportView(pnlLeft);

        splPane.setLeftComponent(srclpLeftAdmin);

        pnlHome.setMaximumSize(null);
        pnlHome.setPreferredSize(new Dimension(50, 50));

        GroupLayout pnlHomeLayout = new GroupLayout(pnlHome);
        pnlHome.setLayout(pnlHomeLayout);
        pnlHomeLayout.setHorizontalGroup(
            pnlHomeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 803, Short.MAX_VALUE)
        );
        pnlHomeLayout.setVerticalGroup(
            pnlHomeLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 1055, Short.MAX_VALUE)
        );
        
        splPane.setRightComponent(pnlHome);

        getContentPane().add(splPane, BorderLayout.CENTER);

        menuFile.setText("File");

        menuItemLogout.setIcon(new ImageIcon(getClass().getResource("/images/logout.png"))); 
        menuItemLogout.setText("Logout");
        menuItemLogout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mnuiLogoutActionPerformed(evt);
            }
        });
        menuFile.add(menuItemLogout);

        menuItemExit.setIcon(new ImageIcon(getClass().getResource("/images/btn_delete.png"))); 
        menuItemExit.setText("Exit Application");
        menuItemExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                mnuiExitActionPerformed(evt);
            }
        });
        menuFile.add(menuItemExit);

        menuBarMain.add(menuFile);

        setJMenuBar(menuBarMain);

        pack();
	}
	
	private void setFrameStyle(){
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;

        setBounds((width - 1024) / 2, (height - 768) / 2, 1024, 768);

        setIconImage((new ImageIcon("wire_transfer.png")).getImage());

        setFont(new Font("Verdana", Font.BOLD, 10));
        splPane.setRightComponent(pnlHome);
	}
	
	private void btnLocationManageActionPerformed(ActionEvent evt) {
        PnlAdminLocationManage pnlLocationManage = new PnlAdminLocationManage();
        splPane.setRightComponent(pnlLocationManage);
    }
	
	private void btnDepartmentManageActionPerformed(ActionEvent evt) {
        PnlAdminDepartmentManage pnlDepartmentManage = new PnlAdminDepartmentManage();
        splPane.setRightComponent(pnlDepartmentManage);
    }
	
	private void mnuiLogoutActionPerformed(ActionEvent evt) {
        logOut();
    }
	
	 private void mnuiExitActionPerformed(ActionEvent evt) {
	        if (JOptionPane.showConfirmDialog(this, "You want to exit ?", "Exit", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
	            System.exit(0);
	        }
	    }
	 
	 private void btnEmployeeManageActionPerformed(ActionEvent evt) {
	        PnlAdminEmployeeManage pnlEmp = new PnlAdminEmployeeManage();
	        splPane.setRightComponent(pnlEmp);
	    }
	 
	 private void formWindowClosing(WindowEvent evt) {
	        Main.frmLogin.setVisible(true);

	    }
	 
	 private void btnProjectManageActionPerformed(ActionEvent evt) {
	        PnlAdminProjectManage pnlPrj = new PnlAdminProjectManage();
	        splPane.setRightComponent(pnlPrj);
	    }
	 
	 private void btnEmpTransferManageActionPerformed(java.awt.event.ActionEvent evt) {
		  

	    }

	    private void btnRequestTransferActionPerformed(java.awt.event.ActionEvent evt) {
	    
	    }

	    private void btnHomeEmpActionPerformed(java.awt.event.ActionEvent evt) {
	    	
	    }
	    
	    private void btnDepartmentListActionPerformed(ActionEvent evt) {
	        PnlUserDepartmentList deptEmpPnl = new PnlUserDepartmentList();
	        splPane.setRightComponent(deptEmpPnl);
	    }
	    
	    private void btnProjectListActionPerformed(ActionEvent evt) {

	    }

	    private void btnLocationListActionPerformed(ActionEvent evt) {
	        PnlUserLocationList empLocLst = new PnlUserLocationList();
	        splPane.setRightComponent(empLocLst);
	    }

	    private void btnLogout1ActionPerformed(java.awt.event.ActionEvent evt) {
	        logOut();
	    }

	    private void btnEmpMyProfileActionPerformed(java.awt.event.ActionEvent evt) {
	   
	    }
	    
	    private void logOut() {
	        if (JOptionPane.showConfirmDialog(null, "You want to exit?", "Exit", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
	            curEmp = null;
	            Main.frmLogin.setVisible(true);
	            this.dispose();
	        }
	    }
	    
	    /*AdminLayout*/
	    private void setAdminLayout() {
	        scrlpLeftUser.setVisible(false);

	        srclpLeftAdmin.setVisible(true);
	        splPane.setLeftComponent(srclpLeftAdmin);

	        this.setTitle(this.getTitle() + " - SYSTEM ADMINISTRATOR");
	    }
	    
	    /*UserLayout*/
	    private void setUserLayout() {
	    	srclpLeftAdmin.setVisible(false);

	    	scrlpLeftUser.setVisible(true);
	    	splPane.setLeftComponent(this.scrlpLeftUser);

	        this.setTitle(this.getTitle() + " - NORMAL USER");
	    }

}
